Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: S3 파일 업로드 베이스 코드 구현 #24

Merged
merged 4 commits into from
Jan 2, 2025
Merged

Conversation

kdh10806
Copy link
Collaborator

@kdh10806 kdh10806 commented Jan 2, 2025

바로 MERGE하지 마세요! 검토 후 MERGE해야합니다!

closes : #23

✅ 최근 작업 주제 (하나 이상의 주제를 선택해주세요.)

  • 기능 추가
  • 리뷰 반영 수정
  • 리팩토링
  • 버그 수정
  • 컨벤션 수정
  • 브랜치 최신화

🏆 구현 목표

S3 파일 업로드 기본 세팅 및 베이스 코드 구현


📋 구현 사항 설명 (작업한 내용을 상세하게 기록합니다.)

  1. 파일 관련 Entity 구현 - FileMetadata, FileMetadataHistory, Link, LinkHistory
  2. S3 관련 Config 파일 구현 - S3Config
  3. S3 관련 application.properties 수정
  4. FileController 구현 - 테스트용 Controller
  5. FileService 구현 - S3 파일 업로드 메서드
  6. S3ClientService 구현 - S3에 업로드 하기위한 기본 클라이언트 서비스 코드
  7. FileMetadataDto 구현
  8. FileMetadataRepository 구현
  9. FileValidator 구현 - 파일 유효성 검사

🔍 테스트 방법 (변경 사항을 확인하기 위한 테스트 방법을 기술합니다.)

  1. Postman으로 요청
  2. S3 버킷에 해당 파일 업로드 되는지 확인
  3. Postman으로 response 값 확인
    스크린샷 2025-01-02 오후 12 41 42

🛠️ 쓰이는 모듈

  • spring-cloud-starter-aws

💬 기타 질문 및 특이 사항

@kdh10806 kdh10806 added the ✨ Feature 기능 개발 label Jan 2, 2025
@kdh10806 kdh10806 self-assigned this Jan 2, 2025
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review by ChatGPT

Comment on lines 51 to 78
FileMetadata metadata = FileMetadata.builder()
.fileCategory(fileCategory)
.fileDisplayTitle(file.getOriginalFilename())
.fileTitle(uniqueFileName)
.writtenBy(uploaderId) //TODO) Audit
.writtenAt(LocalDateTime.now()) //TODO) Audit
.contentType(file.getContentType())
.fileFormat(file.getOriginalFilename().substring(originalFilename.lastIndexOf('.') + 1))
.fileSize(file.getSize() / 1024.0) // KB
.filePath(filePath)
.referenceId(referenceId) // 필요 시 설정
.createdBy(1L) //TODO) Audit
.createdAt(LocalDateTime.now()) //TODO) Audit
.build();

//FileMetaData 저장
FileMetadata savedMetadata = fileMetadataRepository.save(metadata);

//DTO로 변환 후 반환
return FileMetadataDto.toDto(savedMetadata);

} catch (Exception e){
//저장 실패시 S3에서 삭제
s3ClientService.deleteFile(s3Key);

throw new BusinessException(e.getMessage(), ErrorCode.S3_UPLOAD_FAIL_ERROR);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 이 메소드에서는 "S3에 파일 저장" 과 "엔티티 생성", "DB에 엔티티 저장" 이렇게 3가지 일을 하고 있습니다.

엔티티 생성과 DB에 저장하는 것은 크게 한가지 책임으로 볼 수 있지만 S3에 파일을 저장하는건 다른 책임이라고 생각됩니다.

클래스 이름이 FileService 인 만큼 DB에 저장하는 로직은 다른 서비스로 분리 하는것을 추천 드립니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기능 구현 후 리팩토링 과정에서 기능별로 분리하겠습니다.

Comment on lines +39 to +45
return new StringBuilder()
.append(uploaderId)
.append("/")
.append(category)
.append("/")
.append(fileName)
.toString();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 코드를 잘 못 이해한 것 같습니다. 지금 상황이라면 StringJoiner를 사용하는게 더 효과적인것 같습니다.

지금 상황으로도 딱히 문제는 없으므로 참고만 해주시면 좋을 것 같아요.

@kdh10806 kdh10806 merged commit a5a12e9 into dev Jan 2, 2025
3 checks passed
@kdh10806 kdh10806 deleted the kdh/file-upload branch January 2, 2025 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Feature 기능 개발
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feat] S3 파일 업로드 베이스 코드 구현
3 participants